package com.yc.encrypt;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.MD5;
import com.google.common.base.Joiner;
import org.hashids.Hashids;

public class TestMD5 {
	
	/**
	 * 对字符串排序
	 */
	public void sortString() {
		
		List<String> list = new ArrayList<>();
    	list.add("abc");
    	list.add("gijk");
    	list.add("defg");
    	list.add("fgh");
    	Collections.sort(list);
    	System.out.println(list);//[abc, defg, fgh, gijk]
    	
    	List<String> list1 = new ArrayList<>();
    	list1.add("fgh");
    	list1.add("gijk");
    	list1.add("defg");
    	list1.add("abc");
    	Collections.sort(list1);
    	System.out.println(list1);//[abc, defg, fgh, gijk]

    	List<String> list2 = new ArrayList<>();
    	list2.add("gijk");
    	list2.add("defg");
    	list2.add("abc");
    	list2.add("fgh");
    	Collections.sort(list2);
    	System.out.println(list2);//[abc, defg, fgh, gijk]
    	
    	List<String> list3 = new ArrayList<>();
    	list3.add("e433660ad80d48f6a4e7da7fe4d0dfe5");
    	list3.add("f843e366a2754f8cbaa1c1a1569c0192");
    	list3.add("90b971a33d5942d7b473b894d210d7c4");
    	list3.add("c5b9bf7a312a413180fa7e55e1de29dd");
    	Collections.sort(list3);
    	System.out.println(list3);
    	
    	List<String> list4 = new ArrayList<>();
    	list4.add("e433660ad80d48f6a4e7da7fe4d0dfe5");
    	list4.add("f843e366a2754f8cbaa1c1a1569c0192");
    	list4.add("c5b9bf7a312a413180fa7e55e1de29dd");
    	list4.add("90b971a33d5942d7b473b894d210d7c4");
    	Collections.sort(list4);
    	System.out.println(list4);
    	
    	List<String> list5 = new ArrayList<>();
    	list5.add("e433660ad80d48f6a4e7da7fe4d0dfe5");
    	list5.add("90b971a33d5942d7b473b894d210d7c4");
    	list5.add("f843e366a2754f8cbaa1c1a1569c0192");
    	list5.add("c5b9bf7a312a413180fa7e55e1de29dd");
    	Collections.sort(list5);
    	System.out.println(list5);//[90b971a33d5942d7b473b894d210d7c4, c5b9bf7a312a413180fa7e55e1de29dd, e433660ad80d48f6a4e7da7fe4d0dfe5, f843e366a2754f8cbaa1c1a1569c0192]
	}
	
	/**
	 * 获取字符串的md5值
	 * @return
	 */
	public static String getStringMd5(String str) {
		MessageDigest digest = null;  
		try {
			byte[] buffer = str.getBytes();
			digest = MessageDigest.getInstance("MD5");
			digest.update(buffer, 0, buffer.length);  
		} catch (NoSuchAlgorithmException e) {
			System.out.println("MD5读取失败");
			throw new RuntimeException(e);
		} 
		BigInteger bigInt = new BigInteger(1, digest.digest()); 
		return bigInt.toString(16);
	}
	
	public void m2() {
		List<String> list4 = new ArrayList<>();
    	list4.add("e433660ad80d48f6a4e7da7fe4d0dfe5");
    	list4.add("f843e366a2754f8cbaa1c1a1569c0192");
    	list4.add("c5b9bf7a312a413180fa7e55e1de29dd");
    	list4.add("90b971a33d5942d7b473b894d210d7c4");
    	Collections.sort(list4);
    	System.out.println(list4);
    	StringBuffer sb = new StringBuffer();
    	for (String str : list4) {
    		sb.append(str);
		}
    	
    	String joinStr11 = Joiner.on(",").join(list4);
    	System.out.println("joinS11 =" + joinStr11);
    	String joinStr = sb.toString();
    	System.out.println("joinStr =" + joinStr);
    	String md5 = getStringMd5(joinStr);
    	System.out.println("md5=" + md5);
    	
	}


	public void m3() {
//		String str = "{\"order_id\":\"1609314254\",\"order_status\":9,\"order_status_msg\":\"下单成功\"}chediandian";
		String str = "{\"order_id\":\"16093142\",\"order_status\":\"9\",\"order_status_msg\":\"\\u4e0b\\u5355\\u6210\\u529f\"}chediandian";

//		String str = "timestamp=1609917797982&key=1000&secret=e01ac9291327686e41ab12479a00e8e2";
		System.out.println(DigestUtil.md5Hex(str));//81db6e6f30215f09403fb7668293b919
	}

	public void m4() {

		Hashids hashids = new Hashids("chediandian-realgoods-salt");
		//加密
		String encode = hashids.encode(1);
		System.out.println("加密后：" + encode);//加密后：VX
		//解密
		long[] decode = hashids.decode(encode);
		System.out.println("解密后：" + decode[0]);//解密后：1

	}

	public void m5() {

		String V8_SIGN_KEY = "Nfpvtta9ZAHBn48F";
		String token = "0B7E298CDCC44959C405D459B17DBFE6";
		Long time = 1611109880983L;
		String deviceId = "8b758663-669e-4d40-8175-749e124b4438";
		String position = "120.07232499999994,30.295891031867427";
		String s = V8_SIGN_KEY + "-" + token + "-" + time + "-" + deviceId + "-" + position;
		System.out.println(s);
		String res = SecureUtil.md5(s).toUpperCase();
		res = SecureUtil.md5("Nfpvtta9ZAHBn48F-0B7E298CDCC44959C405D459B17DBFE6-1611114885959-8b758663-669e-4d40-8175-749e124b4438-120.07231999999993,30.295896031864018");
		System.out.println(res);//4C75F26450B3644E60CCEC6529DA761C
	}

	public void m6() {
		Long timestamp = System.currentTimeMillis();
		System.out.println(timestamp);//1611212836145
		String str = DigestUtil.md5Hex(StrUtil.format("timestamp={}&key={}&secret={}", timestamp,
				1000, "e01ac9291327686e41ab12479a00e8e2")).toUpperCase();
		System.out.println(str);//11DF13025C7F35118CD5B2BD0A092325
	}

	public void m7() {
		Long timestamp = System.currentTimeMillis();
//		Long timestamp = 1620357964502L;
		System.out.println(timestamp);//1620356064808
		String str="timestamp="+timestamp+"&key=ca7e3bc1ada649beb96011040361e85a";
		String sign=DigestUtil.md5Hex(str, CharsetUtil.UTF_8).toUpperCase();
		System.out.println(sign);//4B649976F9C8E7146052ACEE6E07997C
	}

	public void m8() {
		String MINI_API_SIGN_KEY = "05b36da10a2f9f6a1e28436e43ecce2f";
		String waitSignStr = "AppSourceAlipayAreaId330106CddAppId20005CddUuIdC3031cbb83b21d3c0d7b44a22fbb236cCityId3301Coordinate120.13346424724179,30.280264026138717MobileBrandIPHONEMobileTypeiPhone 6PlatType3PlatVersioniOSSysVersion10.1.80Timestamp1624870143975Version3.9.0.08989DFB03174D268";
		String sign = Base64.encode(SecureUtil.hmacSha1(MINI_API_SIGN_KEY).digest(waitSignStr, CharsetUtil.UTF_8));
		System.out.println(sign);
	}

	public String toMD5(String str, boolean upper) {
		try {
			boolean isUpper = false;
			isUpper = upper;
			String result = "";
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(str.getBytes("UTF-8"));
			byte[] encryption = md5.digest();

			StringBuffer strBuf = new StringBuffer();
			for (int i = 0; i < encryption.length; i++) {
				if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
					strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
				} else {
					strBuf.append(Integer.toHexString(0xff & encryption[i]));
				}
			}

			result = strBuf.toString();
			if (isUpper) {
				result = result.toUpperCase();
			} else {
				result = result.toLowerCase();
			}
			return result;
		} catch (Exception e) {
			return "";
		}
	}

	public void m9() {
		System.out.println(toMD5("123456", true));
	}



	public static void main(String[] args) {
		TestMD5 tmd = new TestMD5();
//		tmd.sortString();
//		tmd.m2();
//		tmd.m5();//a9f82d433dc67412ec247cc533ac370b
//		tmd.m4();//a9f82d433dc67412ec247cc533ac370b
//		tmd.m6();
		tmd.m7();
//		tmd.m8();
//		tmd.m9();
	}

}
